﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WDeviceType : NotificationObject
    {
        private int     deviceTypeUID;
        private String  deviceTypeName;
        private String  deviceStructure;
        private int     deviceVolume;
        private String  deviceSize;
        private String  hasee;

        [DataMember(Order = 0)]
        public int DeviceTypeUID
        {
            get
            {
                return deviceTypeUID;
            }
            set
            {
                deviceTypeUID = value;
                RaisePropertyChanged("DeviceTypeUID");
            }
        }

        [DataMember(Order = 1)]
        public String DeviceTypeName
        {
            get
            {
                return deviceTypeName;
            }
            set
            {
                deviceTypeName = value;
                RaisePropertyChanged("DeviceTypeName");
            }
        }

        [DataMember(Order = 2)]
        public String DeviceStructure
        {
            get
            {
                return deviceStructure;
            }
            set
            {
                deviceStructure = value;
                RaisePropertyChanged("DeviceStructure");
            }
        }

        [DataMember(Order = 3)]
        public int DeviceVolume
        {
            get
            {
                return deviceVolume;
            }
            set
            {
                deviceVolume = value;
                RaisePropertyChanged("DeviceVolume");
            }
        }

        [DataMember(Order = 4)]
        public String DeviceSize
        {
            get
            {
                return deviceSize;
            }
            set
            {
                deviceSize = value;
                RaisePropertyChanged("DeviceSize");
            }
        }

        [DataMember(Order = 5)]
        public String Hasee
        {
            get
            {
                return hasee;
            }
            set
            {
                hasee = value;
                RaisePropertyChanged("Hasee");
            }
        }

        public WDeviceType()
        {
            DeviceTypeUID = -1;
            DeviceTypeName = "";
            DeviceStructure = "";
            DeviceVolume = -1;
            DeviceSize = "";
            Hasee = "";
        }

        public WDeviceType(LDeviceType p)
        {
            DeviceTypeUID = p.DeviceTypeUID;
            DeviceTypeName = p.DeviceTypeName;
            DeviceStructure = p.DeviceStructure;
            DeviceVolume = p.DeviceVolume;
            DeviceVolume = p.DeviceVolume;
            DeviceSize = p.DeviceSize;
            Hasee = p.Hasee;
        }

        public static int GetDeviceTypeUID(String pDeviceTypeName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.DeviceType, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WDeviceType> list = JSON.parse<List<WDeviceType>>(res.Data1);
            foreach (var item in list)
            {
                if (item.DeviceTypeName.Trim() == pDeviceTypeName.Trim())
                {
                    return item.DeviceTypeUID;
                }
            }

            return -1;
        }

        public static List<WDeviceType> GetAll()
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.DeviceType, EMethod.SelectAll, "");
            return JSON.parse<List<WDeviceType>>(res.Data1);
        }

        public static HttpResponse Insert(List<WDeviceType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DeviceType, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WDeviceType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DeviceType, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WDeviceType> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.DeviceType, EMethod.Delete, postData);
        }
    }
}
